package net.towee.client.com.repo.presenter;

import net.towee.client.com.repo.RepositoryNodeCreator;
import net.towee.client.com.repo.RepositoryShower;
import net.towee.client.managers.NodesManager;
import net.towee.model.repo.NodeData;
import net.towee.model.repo.impl.NodeDataImpl;

import com.google.common.base.Preconditions;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

public class RepositoryNodeCreatorPresenter implements RepositoryNodeCreator, ClickHandler, ChangeHandler {

	public interface Display extends IsWidget {
		HasClickHandlers getCreateButton();
		
		HasClickHandlers getCancelButton();
		
		HasChangeHandlers getSearchChanger();
		
		String getNodeName();
		
		void nodeExists();
		
		void nodeNotExists();
	}
	
	// Injected
	private final Display display;
	private final NodesManager nodesManager;
	private final RepositoryShower shower;
	
	private NodeData parentNode;
	private boolean checkState = false;
	
	@Inject
	public RepositoryNodeCreatorPresenter(Display display, NodesManager nodesManager, RepositoryShower shower) {
		this.display = display;
		this.nodesManager = nodesManager;
		this.shower = shower;
		
		display.getCreateButton().addClickHandler(this);
		display.getSearchChanger().addChangeHandler(this);
		display.getCancelButton().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				RepositoryNodeCreatorPresenter.this.display.asWidget().removeFromParent();
			}
		});
	}
	
	@Override
	public void go(HasWidgets container) {
		Preconditions.checkNotNull( parentNode, "null node data" );
		
		// Show creator
		container.add( display.asWidget() );
	}

	@Override
	public void onChange(ChangeEvent event) {
		checkState = false;
		
		// Check node existing in some parent node
		nodesManager.checkNodeExisting( display.getNodeName(), parentNode.getGroupId(), parentNode.getId(), new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {}

			@Override
			public void onSuccess(Boolean result) {
				if(result == true) 
					display.nodeExists();
				else {
					display.nodeNotExists();
					checkState = true;
				}
			}
		} );
	}

	@Override
	public void onClick(ClickEvent event) {
		Preconditions.checkArgument( checkState, "node checking not successfull" );
		
		// Create node object
		NodeData node = new NodeDataImpl(null, parentNode.getGroupId(), display.getNodeName());
		node.setParentId( parentNode.getId() );

		// Create node and show parent
		nodesManager.saveNode(node, new AsyncCallback<NodeData>() {
			@Override
			public void onFailure(Throwable caught) {}

			@Override
			public void onSuccess(NodeData result) {
				// Remove from parent
				display.asWidget().removeFromParent();
			}
		});
	}

	@Override
	public void setParentNode(NodeData parentNode) {
		this.parentNode = parentNode;
	}
}
